<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>

<%@ include file="function.jsp"%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>角色管理</title>
    <link  href="../../hos_manage/images/favicon.ico?cache=<%=Math.random()%>" rel="shortcut icon" type="image/x-icon">
    <link rel="stylesheet" href="../css/public.css?cache=<%=Math.random()%>">
    <link rel="stylesheet" href="../css/messages_doctor_mes.css?cache=<%=Math.random()%>">
    <link rel="stylesheet" href="../css/sweetalert.css?cache=<%=Math.random()%>" >
    <link rel="stylesheet" href="../css/pageCode.css?cache=<%=Math.random()%>" />

    <script src="../js/jquery-1.9.1.min.js"></script>
</head>
<body>
    <%@ include file="../header.jsp"%>
    <!--内容-->
    <div class="containerone">
        <%@ include file="../left_menu.jsp"%>
        <!--右边内容-->
        <div class="right_cont">
        <!--右边导航下面内容-->
            <div id="doctor_detail">
                <div class="message_hos_news_container">
                    <!--左边的渠道-->
                    <div class="department_top">
                        <div class="department_top_left department_top_top">
                            <span>
                                <p class="padding-right-min">角色管理</p>
                            </span>
                            <select v-model="hospital" @change="getRolesList()" v-if="powerChoose">
                                <option :value="i" v-for="i in hospitalList" v-html="i.name"></option>
                            </select>
                        </div>
                        <!--右边的搜索\添加\批量添加-->
                        <div class="department_top_right ui_line_height">
                          <%
                            if (checkAuth("添加角色", input)) {
                          %>
                            <a href="add_role.jsp">
                                <span class="add_btn doctor_add_btn " id='doctor_add_btn'>添加</span>
                            </a>
                          <%
                            }
                          %>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                      <!--标题/发表日期/作者/操作-->
                    <div class="department_detail">
                        <table class="department_detail_table">
                            <tr class='department_detail_tittle'>
                                <td class='ui_widthone'>角色</td>
                                <td class='ui_widthone'>权限</td>
                                <td class='ui_widthone'>用户</td>
                                <td class='ui_widthone'>操作</td>
                            </tr>
                            <input type="hidden" name="deptCode" value="">
                                <tr class="department_detail_c" v-for="role in roles">
                                    <td v-html="role.name"></td>
                                    <td>
                                        <span class="mes_doc_edit doctor_add_btn ">
                                            <%
                                                if (checkAuth("查看角色权限", input)) {
                                            %>
                                                <span class="href" @click="seeRole(role)">查看</span>
                                            <%
                                                }
                                            %>
                                        </span>
                                        <span class="mes_doc_edit doctor_add_btn">
                                            <%
                                                if (checkAuth("编辑角色权限", input)) {
                                            %>
                                                <span class="href"  @click="editRole(role)" v-if="role.id != ${user_role_id}">编辑</span>
                                            <%
                                                }
                                            %>
                                        </span>
                                    </td>
                                    <td>
                                        <span class="mes_doc_edit doctor_add_btn ">
                                        <%
                                            if (checkAuth("查看后台管理员列表", input)) {
                                        %>
                                            <span class="href" @click="user(role)">用户管理</a>
                                        <%
                                            }
                                        %>
                                        </span>
                                    </td>
                                    <td class='ui_color'>
                                    <%
                                        if (checkAuth("删除角色", input)) {
                                    %>
                                        <span class="delete_doc_mes href" id="" @click="removeRole(role)">删除</span>
                                    <%
                                        }
                                    %>
                                    </td>
                                </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="/js/sweetalert-dev.js"></script>
    <script>
        new Vue({
            el: "#doctor_detail",
            data: {
                roles: [],
                hospitalList: [],
                hospital: {},
                powerChoose: hasHospitalPower.power(),
            },
            methods: {
                user: function(role) {
                    locationHerf("user", {
                        role: role
                    });
                },
                seeRole: function(role) {
                    locationHerf("see_role", {
                        role: role
                    });
                },
                editRole: function(role) {
                    locationHerf("edit_role", {
                        role: role
                    });
                },
                removeRole: function(role) {
                    var self = this;
                    swal({
                        title: "您确定要删除该角色吗？",
                        text: '删除该角色后,该角色用户将被一同删除',
                        type: "warning",
                        showCancelButton: true,
                        closeOnConfirm: false,
                        confirmButtonText: "是的，我要删除",
                        cancelButtonText: "取消",
                        confirmButtonColor: "#ec6c62"
                    }, function() {
                        $.ajax({
                            type: 'POST',
                            url: "role.jsp",
                            data: {
                                act: "delete_role",
                                role_id: role.id + "",
                            },
                            success: function(res) {
                                res = JSON.parse(res);
                                if (res.errcode == 0) {
                                    var index = self.roles.indexOf(role);
                                    if (index != -1) {
                                        self.roles.splice(index, 1);
                                        swal("删除成功");
                                    } else {
                                        swal("删除失败");
                                    }
                                } else {
                                    swal(res.errmsg);
                                }
                            },
                        });
                    })
                },
                getRolesList: function() {
                    var self = this;
                    // 触发一次set事件，把数据存放进sessionStorage中
                    if (hasHospitalPower.power()) {
                        hasHospitalPower.nowHospital = this.hospital;
                    }
                    $.ajax({
                        type: 'POST',
                        url: "role.jsp",
                        data: hasHospitalPower.parms({
                            act: "roles",
                        }),
                        success: function(res) {
                            res = JSON.parse(res);
                            if (res.errcode == 0) {
                                self.roles = res.data;
                            } else {
                                swal(res.errmsg);
                            }
                        }.bind(this),
                    });
                }
            },
            ready: function() {
                if (hasHospitalPower.power()) {
                    $.ajax({
                        type: 'POST',
                        url: "role.jsp",
                        data: {
                            act: "hospitals",
                        },
                        success: function(res) {
                            res = JSON.parse(res);
                            if (res.errcode == 0) {
                                this.hospitalList = res.data;
                                this.hospital = this.hospitalList.filter(function(item) {
                                    return item.hospital_id == hasHospitalPower.nowHospital.hospital_id;
                                })[0];
                                this.getRolesList();
                            } else {
                                swal(res.errmsg);
                            }
                        }.bind(this),
                    });
                } else {
                    this.getRolesList();
                }
            }
        })
    </script>
</body>
</html>
